
/* -----------------
* brief 
* 1. this is code gen by tools 
* 2. for add/update/delete/query the data save in sql database   
*/

package merchantCard

import (
	"github.com/go-xorm/xorm"
	"github.com/mjiulee/lego"
	"github.com/mjiulee/lego/utils"
	"saas_sys_base/common"

	//"github.com/mjiulee/lego/utils"
)

/* 根据id获取记录
 * -----------------
 */
func (t *MchMemberCard) RecodeById(id int64) *MchMemberCard {
	item := new(MchMemberCard)
	_, err := lego.GetDBEngine().ID(id).Where("if_del = ?", 0).Get(item)
	if item.Id <= 0 || err != nil{
		return nil
	}
	return item
}

//这里假定一个租户只有一张会员卡，如果有多张的话，就需要查list出来了
func (t *MchMemberCard) RecodeByWho(who *common.Who) *MchMemberCard {
	item := new(MchMemberCard)
	_, err := common.GetDBEngineByTenantWho(who).Get(item)
	//_, err := lego.GetDBEngine().ID(id).Where("if_del = ?", 0).Get(item)
	if item.Id <= 0 || err != nil{
		return nil
	}
	return item
}


func (t *MchMemberCard) RecodeByIdAndWho(appwho *common.Who, id int64) *MchMemberCard {
	item := new(MchMemberCard)

	_, err := common.GetDBEngineByTenantWho(appwho).ID(id).Get(item)
	//lego.GetDBEngine().ID(id).Where("if_del = ?", 0).Get(item)
	if item.Id <= 0 || err != nil{
		return nil
	}
	return item
}

func (t *MchMemberCard) RecodeByWxCardId(cardid string) *MchMemberCard {
	item := new(MchMemberCard)
	item.BaseInfo.CardId = cardid
	_, err := lego.GetDBEngine().Where("if_del = ?", 0).Get(item)
	if item.Id <= 0 || err != nil{
		return nil
	}
	return item
}


func (t *MchMemberCard) RecodeByIdWithsession(session*xorm.Session, id int64) *MchMemberCard {
	item := new(MchMemberCard)
	_, err := session.ID(id).Where("if_del = ?", 0).Get(item)
	if item.Id <= 0 || err != nil{
		return nil
	}
	return item
}

/* 添加
 * -----------------
 */
func (t *MchMemberCard) AddRecode(item2add *MchMemberCard) bool {
	item2add.Id = lego.UUID()
	if item2add.MemberCard.BaseInfo.CardId == "" {
		//由于BaseInfo.CardId和appid配置为唯一标识被配置为唯一组，所以在商户模式时做个特殊处理，将Id值配置为CardId，防止CardId都为0，导致以后的insert操作都插不进
		item2add.MemberCard.BaseInfo.CardId = utils.Int64ToString(item2add.Id)
	}

	_, err := lego.GetDBEngine().Insert(item2add)
	if err != nil {
		lego.LogError(err)
		return false
	}
	return true
}

func (t *MchMemberCard) AddRecodeWithsession(session*xorm.Session,item2add *MchMemberCard) bool {
	item2add.Id = lego.UUID()
	if item2add.MemberCard.BaseInfo.CardId == "" {
		//由于BaseInfo.CardId和appid配置为唯一标识被配置为唯一组，所以在商户模式时做个特殊处理，将Id值配置为CardId，防止CardId都为0，导致以后的insert操作都插不进
		item2add.MemberCard.BaseInfo.CardId = utils.Int64ToString(item2add.Id)
	}

	_, err := session.Insert(item2add)
	if err != nil {
		lego.LogError(err)
		return false
	}
	return true
}

/* 删除(软删除)
 * -----------------
 */
func (t *MchMemberCard) SDelRecodeById(id int64) bool {
	item := new(MchMemberCard)
	item.IfDel = 1
	_, err := lego.GetDBEngine().ID(id).Update(item)
	if err == nil {
		return true
	}
	return false
}

func (t *MchMemberCard) SDelRecodeByIdWithSession(session*xorm.Session, id int64) bool {
	item := new(MchMemberCard)
	item.IfDel = 1
	_, err := session.ID(id).Update(item)
	if err == nil {
		return true
	}
	return false
}

/* 删除(硬删除)*/
/*func (t *MchMemberCard) DelRecodeById(id int64) bool {
	item := new(MchMemberCard)
	_, err := lego.GetDBEngine().ID(id).Delete(item)
	if err != nil {
        lego.LogError(err)
        return false
    }
    return true
}

func (t *MchMemberCard) DelRecodeByIdWithSession(session*xorm.Session,id int64) bool {
	item := new(MchMemberCard)
	_, err := session.ID(id).Delete(item)
	if err != nil {
        lego.LogError(err)
        return false
    }
    return true
}
*/
/* 更新
 * -----------------*/
func (t *MchMemberCard) UpdateRecode(rc *MchMemberCard) bool {
	_, err := lego.GetDBEngine().Id(rc.Id).Update(rc)
	if err != nil {
        lego.LogError(err)
        return false
    }
    return true
}

func (t *MchMemberCard) UpdateRecodeWithSession(session *xorm.Session,rc *MchMemberCard) bool {
	_, err := session.ID(rc.Id).Update(rc)
	if err != nil {
        lego.LogError(err)
        return false
    }
    return true
}


func (t *MchMemberCard) ListByWho(who *common.Who, page, psize int) ([]MchMemberCard, int64, error) {


	start := ((page - 1)*psize)
	resultList := make([]MchMemberCard, 0)

	session := lego.GetDBEngine().Table(MchMemberCard{}).Alias("t1")
	//Join("INNER", "tb_xx", "tb_xx.id = tb_xx.type_id").
	//Where("if_del = ?", 0).
	//Where("t1.if_del = 0 And t1.mini_app_id = ? And t1.tenant_id = ?", who.MiniAppId, who.TenantId).
	session = common.GetDBSessionWithFilterByAppWho(session, who).
		Desc("t1.cdate")


	//Desc("t1.field_xxx").
	//Asc()
	//

	session2 := session.Clone()
	err := session.Limit(psize, start).Find(&resultList) //TODO: 发现这里用FindAndCount()会出错
	if err != nil {
		return nil, 0, err
	}


	total, err := session2.Count()
	if err != nil {
		return nil, 0, err
	}
	return resultList, total, err
}